﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rocket.Core;
using System.Linq.Expressions;

namespace Rocket.Windows.Framework
{
    public partial class ContainerFilter<H, V, D, DTO> : RocketContainer
        , IFormContainerFilter<V, DTO>
        where D : IFormDataFilter<DTO> 
        where V : BaseObject
        where H : IHandler<V>
        where DTO : class
    {
        private IFormDataFilter<DTO> _dataFilterControl;
        public ContainerFilter(D dataFilter, H handler)
        {
            InitializeComponent();
            _handler = (IHandler<V, DTO>)handler;
            _dataFilterControl = dataFilter;
            LoadFilter(dataFilter);
            this.RocketHeight = this.Height +((Control)_dataFilterControl).Height;
            this.adminController1.FilterClick += new EventHandler(adminController1_FilterClick);
            this.adminController1.LoadAllClick += new EventHandler(adminController1_LoadAllClick);
            this.adminController1.RefreshClick += new EventHandler(adminController1_RefreshClick);
            ProgressBarOn = false;

            if (!string.IsNullOrEmpty(GroupBoxName))
                this.groupBoxFiltering.Text = GroupBoxName;
        }

        void adminController1_RefreshClick(object sender, EventArgs e)
        {
            RocketRefresh();
        }

        void adminController1_LoadAllClick(object sender, EventArgs e)
        {
            ReLoadAll();
        }

        void adminController1_FilterClick(object sender, EventArgs e)
        {
            Filter();
        }

        public void LoadFilter(IFormDataFilter<DTO> dataFilter)
        {
            _dataFilterControl = dataFilter;
            UserControl filterControl = (UserControl)dataFilter;
            filterControl.Dock = DockStyle.Fill;
            this.groupBoxFiltering.Controls.Add(filterControl);
        }

        public bool ProgressBarOn
        {
            set
            {
                this.panelProgressB.Visible =
                this.panelProgressL.Visible =
                this.panelProgressR.Visible =
                this.panelProgressTL.Visible =
                this.panelProgressT.Visible = value;
            }
        }

        public Color ProgressBarColor
        {
            set
            {
                this.panelProgressB.BackColor =
                this.panelProgressL.BackColor =
                this.panelProgressR.BackColor =
                this.panelProgressT.BackColor = value;
            }
        }

        public void Filter()
        {
            if (OnDataFiltered != null)
            {
                ProgressBarOn = true;
                Expression<Func<DTO, bool>> _filterExpression
                    = ((IFormDataFilter<DTO>)_dataFilterControl).GetFilterExpression();
                OnDataFiltered(_filterExpression);
                ProgressBarOn = false;
            }
        }

        public void ReLoadAll()
        {
            if (OnDataFiltered != null)
            {
                ProgressBarOn = true;
                OnDataFiltered(null);
                ProgressBarOn = false;
            }
        }

        private IHandler<V, DTO> _handler;
        public IHandler<V, DTO> IHandler
        {
            get { return _handler; }
        }

        private V _bindedView;
        public V BindedView
        {
            get { return _bindedView; }
        }

        public event DataFiltered<DTO> OnDataFiltered;

        public IRocketFilter<BindingSource, DTO> RefRocketFilter
        {
            get { throw new NotImplementedException(); }
        }


        public IRocketViewer<BindingSource> IRocketViewer
        {
            get { return (IRocketViewer<BindingSource>)this._dataFilterControl; }
        }

        public void SetRocketBindingSourceView(V dataSource)
        {
            _bindedView = dataSource;
        }

        public bool RocketRefresh()
        {
            ((RocketBaseData)_dataFilterControl).RocketRefresh();
            return true;
        }

        public bool RocketLoad()
        {
            throw new NotImplementedException();
        }

        public Expression<Func<DTO, bool>> GetFilterExpression()
        {
            return _dataFilterControl.GetFilterExpression();
        }

        public event EventHandler OnRocketTaskCompleted;

        public event RocketBindingSourceSetted OnRocketBindingSourceSetted;

        public BindingSource RocketBindingSource
        {
            get
            {
                return _dataFilterControl.RocketBindingSource;
            }
            set
            {
                _dataFilterControl.RocketBindingSource = value;
            }
        }

        public void SetRocketBindingSource(ref BindingSource bindingSource)
        {
            _dataFilterControl.SetRocketBindingSource(ref bindingSource);
        }

        public void RocketBindingSourceChanged(object sender, EventArgs e)
        {
            _dataFilterControl.RocketBindingSourceChanged(_bindedView, e);
        }

        public string GroupBoxName { get { return _dataFilterControl.GroupBoxName; } set { _dataFilterControl.GroupBoxName = value; } }
    }
}
